package cn.com.woxitech.dev.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;

import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.Const;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.model.gen.WmApp;
import cn.com.woxitech.dev.model.gen.WmCatalog;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.AppService;
import cn.com.woxitech.dev.service.CatalogService;
import cn.com.woxitech.dev.service.LecturePageService;
import cn.com.woxitech.dev.service.LectureService;
import cn.com.woxitech.dev.service.RoomService;
import cn.com.woxitech.dev.service.SMSCodeService;
import cn.com.woxitech.dev.service.UserService;

@RestController
@RequestMapping("/wjt")
public class CatalogController {
	@Autowired
	RoomService roomService;
	@Autowired
	LectureService lectureService;
	@Autowired
	LecturePageService lecturePageService;
	@Autowired
	UserService userService;
	@Autowired
	SMSCodeService smsCodeService;
	@Autowired
	CatalogService catalogService;
	@Autowired
	AppService appService;

	
	@RequestMapping(value="/catalog_rooms",method=RequestMethod.POST)
	public Object catalog_rooms(@RequestBody String body){
		
		Map<String ,Object> mp = new HashMap<String, Object>();
		
		
		WmCatalog h_catalog = this.catalogService.getCatalogByName("热门");
		WmCatalog a_catalog = this.catalogService.getCatalogByName("全部");
		JsonNode jsonNode;
		try {
			jsonNode = JSONUtils.toJson(body);

			String keyword = null;
			String systemType = jsonNode.get("systemType").asText();
			if(jsonNode.has("keyword")){
				keyword = jsonNode.get("keyword").asText();
			}
			String uuid = jsonNode.get("cataloguuid").asText();
			
			String refreshtype = "all";
			if( jsonNode.get("refreshtype") != null){
				//all, head, footer
				refreshtype = jsonNode.get("refreshtype").asText();
			}
			
			String orderby = "new";
			
			if(h_catalog.getUuid().equals(uuid)){
				uuid = a_catalog.getUuid();
				orderby = "hot";
			}
			
			if(jsonNode.get("orderby") != null){
				orderby = jsonNode.get("orderby").asText();
			}
			if(orderby.equals("new")){
				orderby = "createtime";
			}else if(orderby.equals("hot")){
				orderby = "attendeeCount";
			}else if(orderby.equals("live")){
				orderby = "time";
			}else{
				orderby = "createtime";
			}
			
			long timestamp = System.currentTimeMillis();
			if(jsonNode.get("timestamp") != null){
				timestamp = Long.parseLong(jsonNode.get("timestamp").asText());
			}
			
			int pos = 0;
			if(jsonNode.get("pos") != null){
				pos = Integer.parseInt(jsonNode.get("pos").asText());
			}
			
			int size = 8;
			if(jsonNode.get("size") != null){
				size = Integer.parseInt(jsonNode.get("size").asText());
			}
			int newpos = pos;
			
			
			int appid = 1;
			if(jsonNode.get("appid") != null){
				try{
					appid = jsonNode.get("appid").asInt();
				}catch(Exception e){
					appid = 1;
				}
			}
			WmApp wmApp = appService.getWmAppById(appid);
			
			
			//获取rooms的代码需要优化，已减少数据库访问次数以及循环。
			List<String> roomUuidStrs = new ArrayList<String>();
			if(uuid.equals(wmApp.getCatalogfollowuuid())){ // app关注 catalog
				String unionid = jsonNode.get("unionid").asText();
				WmUser u = this.userService.getUserByUnionId(unionid);
				
				roomUuidStrs = roomService.getRoomUuidsByFollowingUsersUuid(u.getUuid());//, wmApp.getDefaultorguuid());
			}else{
				roomUuidStrs = catalogService.getRoomUuidsByOneCatalogUuid(uuid);
			}
			
			
			
			List<WmRoom> rooms = new ArrayList<WmRoom>();
			
			if(roomUuidStrs.size() != 0){
				
				if(refreshtype.equals("all")){
					
					rooms = roomService.getRoomsByUuidList(roomUuidStrs, orderby);
					
				}else if(refreshtype.equals("head")){
					
					newpos = pos = 0;
					rooms = roomService.getHeadRefreshRoomsByUuidList(roomUuidStrs, orderby, pos, size, timestamp,false,keyword);
					
				}else if(refreshtype.equals("footer")){
					
					rooms = roomService.getFooterRefreshRoomsByUuidList(roomUuidStrs, orderby, pos, size, timestamp,false,keyword);
				}
				
			}
			newpos = newpos + rooms.size();
			
			mp.put("pos", newpos);
			mp.put("rooms", rooms);
			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			mp.put("rooms", new ArrayList<WmRoom>());
			
			return APIResultFactory.success(mp);
		} 
		
	}
	@RequestMapping(value="/catalogs",method=RequestMethod.POST)
	public Object catalogs(){
		
		Map<String ,Object> mp = new HashMap<String, Object>();
		
		JsonNode jsonNode;
		try {
			
			int appid = 1; //默认为直播上科大

			return APIResultFactory.success(catalogService.getCatalogs(appid));
		
		}catch(Exception e){
			return APIResultFactory.fail();
		}
		
	}
	
	@RequestMapping(value="/catalogs_app",method=RequestMethod.POST)
	public Object catalogs_app(@RequestBody String body){
		
		Map<String ,Object> mp = new HashMap<String, Object>();
		
		JsonNode jsonNode;
		try {
			jsonNode = JSONUtils.toJson(body);
			
			int appid = 0;
			if(jsonNode.get("appid") != null){
				try{
					appid = jsonNode.get("appid").asInt();
				}catch(Exception e){
					appid = 0;
				}
			}

			return APIResultFactory.success(catalogService.getCatalogs(appid));
		
		}catch(Exception e){
			return APIResultFactory.fail();
		}
		
	}
	
	@RequestMapping(value="/cataloginit",method=RequestMethod.POST)
	public Object cataloginit(@RequestBody String body){
		
		Map<String ,Object> mp = new HashMap<String, Object>();
		
		JsonNode jsonNode;
		try {
			jsonNode = JSONUtils.toJson(body);
			String unionid = jsonNode.get("unionid").asText();
			String orderby = jsonNode.get("orderby").asText();
			
			WmUser user = userService.getUserByUnionId(unionid);
			
			List<String> catalogUuidStrs = catalogService.getCatalogsStrByUuid(user.getUuid());
			
			List<String> roomUuidStrs = catalogService.getRoomUuidsByCatalogUuids(catalogUuidStrs);
			List<WmRoom> rooms = null;
			if(orderby.equals("news")){
				
				rooms = roomService.getRoomsByUuidList(roomUuidStrs, "createtime");
				
			}else if(orderby.equals("hot")){
				
				rooms = roomService.getRoomsByUuidList(roomUuidStrs, "attendeeCount");
				
			}else if(orderby.equals("live")){
				
				rooms = roomService.getRoomsByUuidList(roomUuidStrs, "time");
				
			}else{
				
				rooms = roomService.getRoomsByUuidList(roomUuidStrs, "createtime");
			}
			
			mp.put("rooms", rooms);
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		} 
		
	}
	
}
